/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import DAO.KhachHangDAO;
import DTO.KhachHangDTO;
import DTO.KhachHangDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author THANHLNSE60620
 */
public class ProcessServlet extends HttpServlet {

    private String homepage = "index.html";
    private String welcomepage = "welcome.jsp";
    private String loginpage = "login.html";
    private String searchpage = "search.jsp";

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String button = request.getParameter("btAction");
            if (button.equals("Login")) {   // begin Login
                int CustCode = Integer.parseInt(request.getParameter("username"));

                String url = loginpage;
                if (KhachHangDAO.checkLogin(CustCode)) { // check login
                    HttpSession session = request.getSession(true);
                    session.setAttribute("CustCode", CustCode);
                    url = welcomepage;
                }
                // redirect
                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
            } // end Login
            else if (button.equals("Create New Account")) {// begin Create New Account
                String ten = request.getParameter("txtTen");
                int Innum = Integer.parseInt(request.getParameter("txtInNum"));
                int phone = Integer.parseInt(request.getParameter("txtPhone"));
                String email = request.getParameter("txtEmail");
                int diem = Integer.parseInt(request.getParameter("txtDiem"));

                KhachHangDTO kh = new KhachHangDTO(0, ten, Innum, phone, email, diem);

                String url = loginpage;
                if (KhachHangDAO.insert(kh)) {
                    url = welcomepage;
                }

                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
            } // end Create New Account
            else if (button.equals("Search")) { // begin Search
//                String name = request.getParameter("txtName");
//                // attach result list and send to front end
//                KhachHangDTO[] a = KhachHangDAO.searchLikeByName(name);
//                for (KhachHangDTO b : a) {
//                    System.out.println(b.getCustomerCode());
//                }
//                request.setAttribute("accounts", a);
//                RequestDispatcher rd = request.getRequestDispatcher(searchpage);
//                rd.forward(request, response);


                String name = request.getParameter("txtName");
                KhachHangDTO[] a = KhachHangDAO.searchLikeByName(name);
                request.setAttribute("INFOR", a);
                RequestDispatcher rd = request.getRequestDispatcher(searchpage);
                rd.forward(request, response);
            } // end Search
            else if (button.equals("Delete")) {//start delete



                String id = request.getParameter("id");//tren url
                String searchValue = request.getParameter("searchQueryString");
                boolean result = KhachHangDAO.deleteCustomer(id);
                String url = homepage;

                if (result) {
                    url = "ProcessServlet?btAction=Search&txtName= " + searchValue;

                }
                response.sendRedirect(url);
            }
        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
